<?php
// +----------------------------------------------------------------------
// | 投票程序
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: VoteAction.class.php 122 2011-05-18 09:00:37Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class VoteAction extends BaseAction {
    
	public function index() {
	
		$v  = M('Vote');
		$c  = A('Public');
		$c->getCommentByType(3);
		$timestamp = gmtime();
	
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		C ('VAR_PAGE','p');
		$count = $v->where("vote_visible = 1 AND vote_status = 1 AND vote_endtime <= '$timestamp'")->count();
		$ps = new Page ($count, C('admin_list_num'));
		$list = get_all_vote($ps->firstRow,$ps->listRows);
		$ps->setConfig('theme','<span class="alignleft">%upPage%</span><span class="alignright">%downPage%</span>');
		$ps->setConfig('prev',"&laquo; Previous Page");
		$ps->setConfig('next','Next Page &raquo;');
		$pollpage = $ps->show ();
		
		if (!empty($list)) {
			$this->assign('pollpage', $pollpage);
			$this->assign('list', $list);
		}
		$this->display();
	}

	public function vote() {
		if ($_POST['act']) {
			$id          = intval($_POST['id']);
			$options_id  = trim($_POST['options_id']);
			$view        = intval($_POST['view']);
			$limit       = intval($_POST['limit']);
		    $ip          = get_client_ip();
			if ($limit) {
				if (vote_already_submited($id, $ip)) {
					$this->error('Sorry, you have voted!');
				}
				/* 如果限制ip投票,保存当前ip信息 */
				$vi = M('VoteIp');
				$data_ip = array();
				$data_ip['vote_id']     = $id; 
				$data_ip['ip_ip']       = $ip; 
				$data_ip['ip_addtime']  = gmtime();
				$vi->add($data_ip);	
			}
			save_vote($id,$options_id);
			$vote = get_vote($id);
			$mes .='<ul class="wp-polls-ul">';
			if ($view) {						
				foreach ($vote['content'][$id]['options'] as $c) {
					$mes .= '<li>'.$c['option_name'].' <small>('.$c['percent'].'%, '.$c['option_count'].' Votes)</small><div class="pollbar" style="width: '.$c['option_width'].'%;" title="'.$c['option_name'].' ('.$c['percent'].'% | '.$c['option_count'].' Votes)"></div></li>';
				}
				$mes .= '</ul><p style="text-align: center; font-size:7.5pt;"><span style="color:#FF6600;">Thanks for voting!</span> Total Voters: <strong>'.$vote['content'][$id]['vote_count'].'</strong></p>';
				//$mes = "Thanks for voting!&nbsp;&nbsp;<span style=\"font-size:8pt;\"><a href=\"__APP__/Vote/result/id=$id\">Results >></a></span>";
			} else {
				foreach ($vote['content'][$id]['options'] as $c) {
					if ($vote['content'][$id]['vote_type']) {
						$mes .='<li><input id="option_id_'.$c['option_id'].'" name="option_id" value="'.$c['option_id'].'" type="checkbox" onclick="checkbox(this)"/> <label for="option_id_'.$c['option_id'].'">';
						if ($c['option_url']) {
							$mes .= '<a href="http://'.$c['option_url'].'" rel="external nofollow">'.$c['option_name'].'</a>';
						} else {
							$mes .= $c['option_name'];
						}
					} else {
						$mes .='<li><input id="option_id_'.$c['option_id'].'" name="option_id" value="'.$c['option_id'].'" type="radio" onclick="checkradio(this)"/> <label for="option_id_'.$c['option_id'].'">';
						if ($c['option_url']) {
							$mes .= '<a href="http://'.$c['option_url'].'" rel="external nofollow">'.$c['option_name'].'</a>';
						} else {
							$mes .= $c['option_name'];
						}
					}
				}
				$mes .= '</ul><p style="text-align: center; font-size:7.5pt;color:#FF6600;">Thanks for voting!</p>';
			}
			//unset($_SESSION['_VOTE_IS_DONE_'.$id]);
			//$_SESSION['_VOTE_IS_DONE_'.$id] = true;
			
			$cookietime = 30000000;
			Cookie::set('voted_'.$id.'_'. C('COOKIEHASH'), $id, $cookietime, C('COOKIEPATH'));
			
			$this->ajaxReturn($mes,'',1);
		} else {
			$this->error ('非法操作');
		}
	}
		
}

?>
